package com.ibeetl.admin.core.service;

import com.ibeetl.admin.core.conf.PasswordConfig.PasswordEncryptService;
import com.ibeetl.admin.core.dao.CoreOrgDao;
import com.ibeetl.admin.core.dao.CoreUserDao;
import com.ibeetl.admin.core.entity.CoreOrg;
import com.ibeetl.admin.core.entity.CoreUser;
import com.ibeetl.admin.core.rbac.UserLoginInfo;
import com.ibeetl.admin.core.util.enums.DelFlagEnum;
import com.ibeetl.admin.core.util.enums.GeneralStateEnum;
import com.sdx.exception.LoginFailException;
import com.sdx.exception.LoginFailExceptionCode;
import com.sdx.service.CoreRedisService;
import org.beetl.sql.core.SQLManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
@Transactional
public class CoreUserService {
	@Autowired
	CoreUserDao userDao ;
	
	@Autowired
	CoreOrgDao orgDao;
	
	@Autowired
	PasswordEncryptService passwordEncryptService;

	@Resource
	private CoreRedisService coreRedisService;
	
	@Autowired SQLManager sqlManager;
	
	public UserLoginInfo login(String userName,String password){
		CoreUser query = new CoreUser();
		query.setCode(userName);
		String passKey = "pass_"+password;
		Long times = coreRedisService.getAtomic(passKey);
		if (times>3) {
			throw new LoginFailException(LoginFailExceptionCode.LOGIN_FAIL,"操作太频繁，请稍后重试！");
		}
		String userKey = "username_"+userName;
		times = coreRedisService.getAtomic(userKey);
		if (times>3) {
			throw new LoginFailException(LoginFailExceptionCode.LOGIN_FAIL,"密码输入错误超过3次，账号被锁定，请30分钟重试！");
		}
		CoreUser user = userDao.templateOne(query);
		if (user==null) {
			//可能是通用密码攻击
			coreRedisService.getAndIncr(passKey,30);
		}

		if (user!=null) {
			String passEncode = passwordEncryptService.password(password, user.getId().intValue());
			if (!passEncode.equalsIgnoreCase(user.getPassword())) {
				//密码错误，记录重试次数
				coreRedisService.getAndIncr(userKey,30);
				user = null;
			}

		}

		if(user==null) {
			throw new LoginFailException(LoginFailExceptionCode.NOT_FOUND,"用户"+userName+"不存在或者密码错误");
		}
		if(!user.getState().equals(GeneralStateEnum.ENABLE.getValue())){
			throw new LoginFailException(LoginFailExceptionCode.USER_FAIL,"用户"+userName+"已经失效");
		}
		if(user.getDelFlag()==DelFlagEnum.DELETED.getValue()) {
			throw new LoginFailException(LoginFailExceptionCode.USER_DEL,"用户"+userName+"已经删除");
		}

		coreRedisService.resetAtomic(passKey);
		coreRedisService.resetAtomic(userKey);
		List<CoreOrg>  orgs = getUserOrg(user.getId(),user.getOrgId());
		UserLoginInfo loginInfo = new UserLoginInfo();
		loginInfo.setOrgs(orgs);
		loginInfo.setUser(user);
		return loginInfo;
		
	}
	
	
	
	public  List<CoreOrg> getUserOrg(long userId,long orgId){
		List<CoreOrg> orgs =  orgDao.queryOrgByUser(userId);
		if(orgs.isEmpty()){
			//没有赋值任何角色，默认给一个所在部门
			CoreOrg userOrg = orgDao.unique(orgId);
			orgs.add(userOrg);
		}
		return orgs;
	}
	
	
	
	public List<CoreUser> getAllUsersByRole(String role){
		return userDao.getUserByRole(role);
	}
	
	public CoreUser getUserByCode(String userName){
		CoreUser user = new CoreUser();
		user.setCode(userName);
		return userDao.templateOne(user);
	}
	
	public void update(CoreUser user){
		userDao.updateById(user);
	}
	public void updateTemplate(CoreUser user) {
		userDao.updateTemplateById(user);
	}
	public CoreOrg getOrgById(Long orgId){
		return orgDao.unique(orgId);
	}
	
	public CoreUser getUserById(Long userId){
		return userDao.unique(userId);
	}
	
	public List<String> getOrgCode(List<Long> orgIds){
		return orgDao.queryAllOrgCode(orgIds);
	}
}
